package com.ykw.parking.controller;

import com.ykw.parking.Utils.Md5;
import com.ykw.parking.Utils.RandomId;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.User;
import com.ykw.parking.service.MailServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class ChangeEmailController {

    @RequestMapping("/changeEmail")
    public String changeEmail(){
        return "changeEmail";
    }
    @Autowired
    MailServiceImpl mailService;
    @RequestMapping(value = "/sendVerificationCode")
    @ResponseBody
    public String sendVerificationCode(String Email, HttpSession session){
        System.out.println(Email);
        User user = userMapper.queryUserbyemail(Email);
        if (user==null){
            return "-1";
        }
        String randomNumber = RandomId.getRandomNumberByNum(6);
        System.out.println(randomNumber);
        session.setAttribute("VerificationCode",randomNumber);
        session.setAttribute("time",System.currentTimeMillis());
        try {
            mailService.sendInlineResourceMail(Email,randomNumber);
            return "1";
        } catch (MessagingException e) {
            return "0";
        }
    }
    @RequestMapping(value = "/submitVerificationCode")
    @ResponseBody
    public String submitVerificationCode(String email,String VerificationCode,HttpSession session){
        System.out.println(VerificationCode);
        long time= System.currentTimeMillis();
        String verificationCode = (String)session.getAttribute("VerificationCode");
        long time1 = (long)session.getAttribute("time");
        if(time-time1>180000){
            session.removeAttribute("VerificationCode");
            session.removeAttribute("time");
            return "-1";
        }
        if(VerificationCode.equals(verificationCode)){
            User user = userMapper.queryUserbyemail(email);
            String pwd=RandomId.getRandomNumberByNum(10);
            try {
                mailService.sendInlineResourceText(email,pwd);
                String code = Md5.code(pwd);
                userMapper.updateUserPwd(code,user.getId());
            }catch (MessagingException e){
                e.printStackTrace();
            }
            return "1";
        }else{
            return "0";
        }
    }
    @RequestMapping(value = "/sendNewEmail")
    @ResponseBody
    public String sendNewEmail(String Email, HttpSession session){
        String randomNumber = RandomId.getRandomNumberByNum(6);
        System.out.println(randomNumber);
        session.setAttribute("VerificationCode",randomNumber);
        session.setAttribute("time",System.currentTimeMillis());
        try {
            mailService.sendInlineResourceMail(Email,randomNumber);
            return "1";
        } catch (MessagingException e) {
            return "0";
        }
    }
    @Autowired
    UserMapper userMapper;
    @RequestMapping(value = "/submitVerificationEmail")
    @ResponseBody
    public String submitVerificationEmail(String VerificationCode,String Email,HttpSession session){
        System.out.println(VerificationCode);
        long time= System.currentTimeMillis();
        String verificationCode = (String)session.getAttribute("VerificationCode");
        long time1 = (long)session.getAttribute("time");
        if(time-time1>180000){
            session.removeAttribute("VerificationCode");
            session.removeAttribute("time");
            return "-1";
        }
        if(VerificationCode.equals(verificationCode)){
            String username =(String)session.getAttribute("username");
            User user = userMapper.queryUserbyname(username);
            userMapper.updateUserEmail(user.getId(),Email);
            Subject subject= SecurityUtils.getSubject();
            subject.logout();
            return "1";
        }else{
            return "0";
        }
    }
}
